<?php

require_once dirname(__FILE__) . '/../lib/cuentas_corrientesGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/cuentas_corrientesGeneratorHelper.class.php';

/**
 * cuentas_corrientes actions.
 *
 * @package    tercermilenio
 * @subpackage cuentas_corrientes
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class cuentas_corrientesActions extends autoCuentas_corrientesActions {
    const OPE_INGRESO = 1;
    const OPE_EGRESO = 2;

    protected function buildCriteria() {
        if (is_null($this->filters)) {
            $this->filters = $this->configuration->getFilterForm($this->getFilters());
        }

        $criteria = $this->filters->buildCriteria($this->getFilters());

        $criteria = new Criteria();
        $criteria->addJoin(ClientesPeer::CLI_ID, CuentasCorrientesPeer::CTA_CTE_CLIENTE);
        $criteria->addGroupByColumn(ClientesPeer::CLI_ID);

        $this->addSortCriteria($criteria);

        $event = $this->dispatcher->filter(new sfEvent($this, 'admin.build_criteria'), $criteria);
        $criteria = $event->getReturnValue();

        return $criteria;
    }

    public function executeEdit(sfWebRequest $request) {

        $this->cuentas_corrientes = $this->getRoute()->getObject();
        $this->clientes = $this->getRoute()->getObject()->getClientes();
        $this->form = $this->configuration->getForm($this->cuentas_corrientes);

        $this->form->setDefault('deuda_hidden', $this->cuentas_corrientes->getCtaCteDeuda());

    }

    public function executeUpdate(sfWebRequest $request) {

        $this->cuentas_corrientes = $this->getRoute()->getObject();
        $this->clientes = $this->getRoute()->getObject()->getClientes();
        $this->form = $this->configuration->getForm($this->cuentas_corrientes);

        $this->processForm($request, $this->form);

        $this->form->setDefault('cta_cte_deuda', $this->cuentas_corrientes->getCtaCteDeuda());
        $this->form->setDefault('deuda_hidden', $this->cuentas_corrientes->getCtaCteDeuda());
        $this->form->setDefault('cli_credito', $this->clientes->getCliCredito());

        $this->setTemplate('edit');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {

        $formParameters = $request->getParameter($form->getName());

        $cuenta_corriente = $form->getObject();
        $deuda = $cuenta_corriente->getCtaCteDeuda();
        $cliente = $cuenta_corriente->getClientes();
        $formParameters['cli_credito'] = $cliente->getCliCredito();
        $formParameters['cta_cte_deuda'] = $deuda;

        $form->bind($formParameters, $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'La deuda fue pagada exitosamente.';

            $cuenta_corriente = $form->getObject();
            $cliente = $cuenta_corriente->getClientes();
            
            $cuenta_corriente->setDeuda($formParameters['cta_cte_pago'], $form->getValue('cta_cte_fecha_limite'));
            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $cuenta_corriente)));

            if ($request->hasParameter('_save_and_add')) {
                $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                $this->redirect('@cuentas_corrientes_new');
            } else {
                $this->getUser()->setFlash('notice', $notice);

                $this->redirect('@cuentas_corrientes');
            }
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

    public function executeAutocompletarClientePorNombre($request) {
        $this->getResponse()->setContentType('application/json');
        $keywords = ClientesPeer::autocompletarClientePorNombre($request->getParameter('q'), $request->getParameter('limit'));
        return $this->renderText(json_encode($keywords));
    }

    public function executeAutocompletarClientePorDocumento($request) {
        $this->getResponse()->setContentType('application/json');
        $keywords = ClientesPeer::autocompletarClientePorDocumento($request->getParameter('q'), $request->getParameter('limit'));
        return $this->renderText(json_encode($keywords));
    }

}
